// pages/course/offlineCourse/applyList/applyList.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navType: 0,
        navList: ['全部', '待付款', '未使用', '已使用', '已取消'],
        orderList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        isBuyIng: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            navType: options.navType
        })
        this.getOrderList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getOrderList()
    },

    onUnload: function() {
        wx.switchTab({
            url: '/pages/user/userCenter/userCenter'
        })
    },

    getOrderList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let orderList = this.data.orderList
        let data
        if (this.data.navType == 0) {
            data = {
                type: 2,
                is_pay: 1,
                page: this.data.nowPage,
                limit: 10
            }
        } else if (this.data.navType == 1) {
            data = {
                stutas: 1,
                type: 2,
                page: this.data.nowPage,
                limit: 10
            }
        } else if (this.data.navType == 2) {
            data = {
                stutas: 2,
                type: 2,
                page: this.data.nowPage,
                limit: 10
            }
        } else if (this.data.navType == 3) {
            data = {
                stutas: 3,
                type: 2,
                page: this.data.nowPage,
                limit: 10
            }
        } else if (this.data.navType == 4) {
            data = {
                stutas: 4,
                type: 2,
                page: this.data.nowPage,
                limit: 10
            }
        }
        common.post('Online/get_online_buy_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    orderList: orderList,
                    isAll: true
                })
            } else {
                orderList = orderList.concat(res)
                that.setData({
                    orderList: orderList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    selectNav: function(e) {
        if (this.data.navType == e.currentTarget.dataset.type) {
            return
        }
        if (this.data.isLoading) {
            return
        }
        this.setData({
            navType: e.currentTarget.dataset.type,
            orderList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getOrderList()
    },

    goDetail: function(e) {
        wx.navigateTo({
            url: '/pages/course/offlineCourse/applyDetail/applyDetail?orderId=' + e.currentTarget.dataset.id
        })
    },

    goCancle: function(e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认取消订单？',
            success(res) {
                if (res.confirm) {
                    let data = {
                        online_buy_id: e.currentTarget.dataset.id
                    }
                    common.post('Online/dcancel_online', data).then(function(res) {
                        if (res.code == 204) {
                            wx.showToast({
                                title: res.error,
                                icon: 'none',
                                duration: 2000
                            })
                        } else {
                            that.setData({
                                navType: 4,
                                orderList: [],
                                nowPage: 0,
                                isLoading: false,
                                isAll: false
                            })
                            that.getOrderList()
                        }
                    }).catch(function(e) {})
                }
            }
        })
    },

    goPay: function(e) {
        if (this.data.isBuyIng) {
            wx.showToast({
                title: '正在支付，请勿重复操作',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            isBuyIng: true
        })
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    wx.setStorageSync('code', res.code)
                    //发起网络请求
                    let data = {
                        online_buy_id: e.currentTarget.dataset.id,
                        code: res.code,
                        pay_client: 1
                    }
                    common.post('Online/pay_again_online', data).then(function(res) {
                        wx.login({
                            success(res) {
                                wx.setStorageSync('code', res.code)
                            }
                        })
                        that.setData({
                            isBuyIng: false
                        })
                        if (res.code == 204) {
                            wx.showToast({
                                title: res.error,
                                icon: 'none',
                                duration: 2000
                            })
                        } else {
                            wx.requestPayment({
                                timeStamp: res.timeStamp,
                                nonceStr: res.nonceStr,
                                package: res.package,
                                signType: res.signType,
                                paySign: res.paySign,
                                success(res) {
                                    that.setData({
                                        navType: 2,
                                        orderList: [],
                                        nowPage: 0,
                                        isLoading: false,
                                        isAll: false
                                    })
                                    that.getOrderList()
                                },
                                fail(res) {
                                    wx.showToast({
                                        title: '支付失败',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                }
                            })
                        }
                    }).catch(function(e) {})
                }
            }
        })
    },

    goComment: function(e) {
        wx.navigateTo({
            url: '/pages/course/comment/comment?courseId=' + e.currentTarget.dataset.id + '&courseType=' + e.currentTarget.dataset.type
        })
    }
})